package com.example.plugin;

import com.example.entity.QuestionDetail;
import com.example.utils.QuestionDetailUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.CaretModel;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.colors.EditorFontType;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.ui.EditorTextField;

public class AddQuestion extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {

        // 获取当前编辑器
        Editor editor = e.getData(CommonDataKeys.EDITOR);
        //获取当前项目
        Project project = e.getProject();
        if (project == null) {
            Messages.showInfoMessage("No project available.", "Error");
            return;
        }
        if (editor == null) {
            Messages.showInfoMessage("No editor available.", "Error");
            return;
        }
        // 获取当前文件的 PsiFile
        VirtualFile virtualFile = FileEditorManager.getInstance(project).getSelectedFiles()[0];
        PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
        if (psiFile == null) {
            return;
        }
        // 获取文档对象
        Document document = editor.getDocument();

        Long questionId = 0L;
        SelectionModel selectionModel = editor.getSelectionModel();
        if (selectionModel.hasSelection()) {
            // 获取选中的文本
            String selectedText = selectionModel.getSelectedText();
            if (selectedText != null) {
                questionId = Long.valueOf(selectedText);
            }
        }

        QuestionDetail questionDetail = QuestionDetailUtil.getQuestionById(questionId);



        String func = questionDetail.getDefaultCode();
        String main_func = questionDetail.getMainFunc();
        String questionCase =questionDetail.getQuestionCase();


        int index = func.lastIndexOf('}');
        String ret = "\n\n/*题目描述：\n\n"+ questionDetail.getContent() +"\n*/\n\n\n"+
                func.substring(0,index) +"\n\n\n"+
                main_func+
                "\n/* 测试用例如下：\n"  + questionCase + "\n*/\n" +
                "\n}";

        ret = ret.replaceAll("\r\n","\n");

//        Messages.showInfoMessage(ret, "Error");

        //在光标位置插入多行代码
        EditorTextField editorTextField = new EditorTextField(ret, project, psiFile.getFileType());
        editorTextField.setFont(editor.getColorsScheme().getFont(EditorFontType.PLAIN));
        editorTextField.setBackground(EditorColorsManager.getInstance().getGlobalScheme().getDefaultBackground());
        editorTextField.setOneLineMode(false);

        // 获取 EditorTextField 的内容
        String textToInsert = editorTextField.getText();
        // 获取光标位置
        CaretModel caretModel1 = editor.getCaretModel();
        int caretOffset1 = caretModel1.getOffset();

        // 在光标位置插入内容
        WriteCommandAction.runWriteCommandAction(project, () -> {
            document.setText( textToInsert);
        });


    }
}











